﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using mojoPortal.Business.WebHelpers;
using mojoPortal.Business;
using System.ComponentModel;
using mojoPortal.Web.UI;

namespace blackhouse.Aparts.Web.Modules {
	public class BodyCheckPanel : Panel {

		#region Properties

		[Browsable(true)]
		public string DisableForBodyClass { get; set; }

		[Browsable(true)]
		public string DisableForHttpContextItem { get; set; }

		#endregion

		#region Fields

		private string pageBodyClass = "";
		private bool contextDisabled = false;

		#endregion

		#region Event Handlers

		protected override void OnInit(EventArgs e) {
			PageSettings pageSettings = CacheHelper.GetCurrentPage();
			if (pageSettings != null && this.Page is CmsPage)
				pageBodyClass = pageSettings.BodyCssClass;

			if (!string.IsNullOrEmpty(this.DisableForHttpContextItem) && HttpContext.Current.Items.Contains(this.DisableForHttpContextItem)) {
				contextDisabled = (bool)HttpContext.Current.Items[this.DisableForHttpContextItem];
			}

			if (this.CheckDisabled()) {
				this.Visible = false;
				this.Enabled = false;
				this.Controls.Clear();
			}

			base.OnInit(e);
		}

		#endregion

		#region Methods

		private bool CheckDisabled() {

			if (this.contextDisabled) return true;

			string[] disParts = DisableForBodyClass.Split(',');
			foreach (string dis in disParts) {
				if (pageBodyClass.Contains(dis.Trim()))
					return true;
			}
			return false;
		}

		#endregion

	}
}